import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import '../config/Config.dart';
import '../services/UserInfo.dart';
import '../services/SignServices.dart';
import '../services/ScreenAdapter.dart';

// 消息弹框
import 'package:fluttertoast/fluttertoast.dart';

// 我的订单
class OrderPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => OrderState();
}

class OrderState extends State {
  List orderListData = [];

//  Map tempList = {
//    "_id": "5fb56a0c1d54febeecf551d5",
//    "uid": "5fb122e845a74ba51c20ca7c",
//    "name": "林黛玉",
//    "phone": "13255664477",
//    "address": "北京市/北京城区/东城区 姑苏",
//    "all_price": "864.0", "pay_status": 0,
//    "order_status": 0,
//    "order_item": [{"_id": "5fb56a0c1d54febeecf551d6",
//      "order_id": "5fb56a0c1d54febeecf551d5",
//      "product_title": "八匹狼羽绒服男--有属性",
//      "product_id": "5a0432f4010e71123466144c", "product_price": 288,
//      "product_img": "public\upload\agbBbaTUWBnWD7pYQOyJsNgJ.png",
//      "product_count": 1, "selected_attr": "185，红色", "add_time": 1605724684646},
//      {
//        "_id": "5fb56a0c1d54febeecf551d7",
//        "order_id": "5fb56a0c1d54febeecf551d5",
//        "product_title": "八匹狼羽绒服男--有属性",
//        "product_id": "5a0432f4010e71123466144c",
//        "product_price": 288,
//        "product_img": "public\upload\agbBbaTUWBnWD7pYQOyJsNgJ.png",
//        "product_count": 2,
//        "selected_attr": "175，红色",
//        "add_time": 1605724684653
//      }
//    ]};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('我的订单')),
      body: Stack(
        children: [
          ListView(
            children: [
              // 订单列表
              Container(
                margin: EdgeInsets.only(top: ScreenAdapter.height(100)),
                child: Column(
                  children: [
                    Column(
                      // 第一层数据
                      children: orderListData.map((item) {
                        List orderItem2 = item['order_item'];

                        // 第二层数据
                        return Container(
                          color: Color.fromRGBO(200, 200, 200, 0.2),
                          padding: EdgeInsets.all(10),
                          child: Column(
                            children: orderItem2.map((item2) {
                              String pic = item2['product_img'];

                              return Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius:
                                  BorderRadius.all(Radius.circular(5)),
                                ),
                                child: Column(
                                  children: [
                                    orderItem2.indexOf(item2) == 0 ? Container(
                                      alignment: Alignment.topLeft,
                                      margin: EdgeInsets.all(20),
                                      child: Row(
                                        children: [
                                          Text('订单编号：'),
                                          Expanded(
                                            flex: 1,
                                            child: Text(
                                              '${item2['order_id']}',
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ) : Container(height: 0),
                                    Container(
                                      padding: EdgeInsets.only(top: 20,bottom: 20),
                                      decoration: BoxDecoration(
                                        border: Border(
                                          top: BorderSide(color: Color.fromRGBO(200, 200, 200, 0.2)),
                                          bottom: BorderSide(color: Color.fromRGBO(200, 200, 200, 0.2)),
                                        ),
                                      ),
                                      child: ListTile(
                                        leading: Image.network(
                                            "${Config.domain + pic.replaceAll(RegExp(r'\\'), '/')}"),
                                        title: Text('${item2['product_title']}'),
                                        trailing: Text('x ${item2['product_count']}'),
                                        onTap: () {
                                          // 跳转到订单详情页
                                          Navigator.pushNamed(context, '/orderDetail',
                                              arguments: {"orderInfo": item});
                                        },
                                      ),
                                    ),
                                    orderItem2.indexOf(item2) == orderItem2.length -1 ? Container(
                                      margin: EdgeInsets.only(top: 20,bottom: 20),
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                                        children: [
                                          Text(
                                              '合计：￥${item['all_price']}元'),
                                          InkWell(
                                            child: Container(
                                              alignment: Alignment.center,
                                              width: ScreenAdapter.width(180),
                                              height: ScreenAdapter.height(80),
                                              decoration: BoxDecoration(
                                                color: Colors.black12,
                                                borderRadius: BorderRadius.circular(5),
                                              ),
                                              child: Text('申请售后'),
                                            ),
                                            onTap: (){},
                                          )
                                        ],
                                      ),
                                    ) : Container(height: 0),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                        );
                      }).toList(),
                    )
                  ],
                ),
              )
            ],
          ),

          // 顶部筛选栏
          Positioned(
            top: 0,
            width: ScreenAdapter.width(750),
            height: ScreenAdapter.width(100),
            child: Container(
              padding: EdgeInsets.only(top: 14, bottom: 14),
              color: Colors.white,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Text('全部'),
                  Text('待付款'),
                  Text('待收款'),
                  Text('已完成'),
                  Text('已取消'),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  initState() {
    super.initState();

    // 获取订单数据
    _getOrderList();
  }

  // 获取订单数据
  _getOrderList() async {
    var api = Config.domain + 'api/orderList';
    var userInfo = await UserInfoServices.getUserInfo();
    var tempJson = {
      "uid": userInfo[0]['_id'],
      "salt": userInfo[0]['salt'],
    };

    var sign = SignServices.getSign(tempJson);

    var response = await Dio().get(api, queryParameters: {
      "uid": userInfo[0]['_id'],
      "sign": sign,
    });

    var res = response.data;
    if (res['success']) {
      this.setState(() {
        orderListData = res['result'];
      });
    } else {
      // 弹框提示
      Fluttertoast.showToast(
        msg: "获取订单数据失败",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
    }
  }
}
